// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import Foundation
import Shared

private let log = Logger.browserLogger

struct MalwareList {
    /// A file name for a given list text file. Must not not contain .txt extension.
    let name: String
    
    /// Malware list may come up in a format that requires us to parse the file to get list of domains.
    /// For example adblock rules use their filter rules. If regex is nil we assume that the list only includes
    /// domains separated by newlines and we do not to any custom parsing. Please keep in mind if you want to provide
    /// a regex for a blocklist, it has to extract only the domain part, any excessive symbols must be removed
    /// by using the regex syntax.
    let regex: NSRegularExpression?
    
    static func generate() -> [MalwareList] {
        var lists = [MalwareList]()
        
        // Adblock Plus scam domains
        
        // Adblock's domains come in format ||domain.com^
        let adblockListRegex = "(?<=\\|\\|).+?(?=\\^)"
        let adblockListName = "adblock-list"
        
        do {
            let regex = try NSRegularExpression(pattern: adblockListRegex, options: .caseInsensitive)
            lists.append(MalwareList(name: adblockListName, regex: regex))
        } catch {
            log.error("Unable to parse list: \(adblockListName), error: \(error)")
        }
        
        // Disconnect's malware list
        let disconnectMalwareListName = "simple_malware"
        lists.append(MalwareList(name: disconnectMalwareListName, regex: nil))
        
        return lists
    }
}
